package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.netca.bmps.userservice.bo.CertInfo;
import net.netca.bmps.userservice.bo.CertInfoLocal;
import net.netca.bmps.userservice.service.UserService;
import net.netca.bmps.userservice.servicelayer.UserServiceImpl;
import net.netca.gdltax.bpms.businessmanage.utilits.Converter;
import net.netca.gdltax.bpms.webservice.CertServiceClient;
import net.netca.gdltax.bpms.webservice.CertServicePortType;
import net.netca.testwebservice.bo.XMLConfig;
import net.netca.util.encoders.Base64;

@SuppressWarnings("serial")
public class UpdateAndInstall extends HttpServlet {
	public UpdateAndInstall() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String signcertdigest=request.getParameter("signcertdigest");
		int validity=Integer.parseInt(request.getParameter("validity"));
		int type = Integer.parseInt(request.getParameter("type"));
		String serialnum = request.getParameter("serialnum");
		
		String reqno = "";
		String encpair = null;
		String pemenccert = null;
		String pemsigncert = null;
		CertInfoLocal certInfo = new CertInfoLocal(); // 本地证书对象
		UserService userService = new UserServiceImpl();

		try {
			CertServiceClient client = new CertServiceClient(XMLConfig
					.getInstance().getUrl());
			CertServicePortType service = client.getCertServiceHttpPort();

			// 证书信息
			CertInfo info = service.updateInstallCert(signcertdigest, validity,
					type, serialnum);
			
			if (info != null) {
				Converter.CertInfoConvert(certInfo, info);

				if (certInfo.getSigncert() != null) {
					certInfo.setX509SignCert(userService.getNetCACert(certInfo
							.getSigncert()));
				}
				if (certInfo.getEnccert() != null) {
					certInfo.setX509EncCert(userService.getNetCACert(certInfo
							.getEnccert()));
				}
				if (certInfo != null) {
					if (certInfo.getX509SignCert() != null || certInfo.getX509EncCert() != null) {
						byte[] bsigncert = certInfo.getX509SignCert().getEncodeValue();
						byte[] benccert = certInfo.getX509EncCert().getEncodeValue();
						byte[] bencpair = certInfo.getEnckeypair();
						reqno = certInfo.getReqno();
						pemsigncert = Base64.encodeToString(bsigncert, 0,
								bsigncert.length, Base64.BASE64_FLAG_NOCRLF);
						pemenccert = Base64.encodeToString(benccert, 0,
								benccert.length, Base64.BASE64_FLAG_NOCRLF);

						if (bencpair == null || bencpair.length == 0) {
							encpair = "";
						} else {
							encpair = Base64.encodeToString(bencpair, 0,
									bencpair.length, Base64.BASE64_FLAG_NOCRLF);
						}
					}
				}

			}

			// 安装时需要的数据
			String userPin = "";
			request.getSession().setAttribute("pemsigncert", pemsigncert);
			request.getSession().setAttribute("pemenccert", pemenccert);
			request.getSession().setAttribute("encpair", encpair);
			request.getSession().setAttribute("certId", certInfo.getId());
			request.getSession().setAttribute("reqno", reqno);
			request.getSession().setAttribute("userPin", userPin);

			response.setCharacterEncoding("UTF-8");
			response.sendRedirect("../install.jsp");
		} catch (Exception e) {
			e.printStackTrace();
			request.getSession().setAttribute("exception", e);
			response.sendRedirect("../showerror.jsp");
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	public void init() throws ServletException {
	}
}
